//
//  ShowAlbumPhotos.swift
//  didigo
//
//  Created by JS_Coder on 2019/3/1.
//  Copyright © 2019 lifayu. All rights reserved.
//

import UIKit
import Photos
import AssetsLibrary

class ShowAlbumPhotos: XOClubPublishController, UIImagePickerControllerDelegate,UINavigationControllerDelegate {
    let maximoPhotos = 9
    
    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func showCamera() {
        let picker = UIImagePickerController.init()
        picker.sourceType = .camera
        picker.modalPresentationStyle = .currentContext
        picker.delegate = self
        self.present(picker, animated: true, completion: nil)
    }

    override func publishButtonAction() {
        super.publishButtonAction()
    }

    override func showAlbumPhotos() {
        // 获取相册管理者<系统>
        let phManager = PHImageManager.default()
        var photos = [UIImage]()
        let maximoCount = maximoPhotos - (self.imageArr.count - 1)
        _ = self.presentHGImagePicker(maxSelected: maximoCount) { (assets: [PHAsset]) in
            for asset in assets{
                let height = CGFloat(asset.pixelHeight)
                let width = CGFloat(asset.pixelWidth)
                var targetSize: CGSize?
                if height / UIScreen.main.bounds.width >= width / UIScreen.main.bounds.width{// scale height
                    let scale = UIScreen.main.bounds.height / height
                    targetSize = CGSize(width: width * scale, height: height * scale)
                }else{ // scale width
                    let scale = UIScreen.main.bounds.width / width
                    targetSize = CGSize(width: width * scale, height: height * scale)
                }
                let options = PHImageRequestOptions()
                options.deliveryMode = .highQualityFormat
                options.resizeMode = .exact
                options.isSynchronous = true
                phManager.requestImage(for: asset, targetSize: targetSize!, contentMode: PHImageContentMode.aspectFill, options: options, resultHandler: { (targetImage, info) in
                    photos.append(targetImage!)
                })
            }
            self.imageArr.addObjects(from: photos)
            self.collectionView.reloadData()
        }
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage{
            if let reduceData = image.jpegData(compressionQuality: 0.5){
                if let newImage = UIImage.init(data: reduceData) {
                    self.imageArr.add(newImage)
                    self.collectionView.reloadData()
                }
            }
        }
        picker.dismiss(animated: true, completion: nil)
    }
}
